<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools>
        <template #slot-left>
          共{{ total }}条记录
        </template>
        <template #slot-right>
          <el-button v-power="'import'" type="danger" size="small" @click="$router.push('/excle')">导入excel</el-button>
          <el-button v-power="'export'" type="success" size="small" @click="downloadExcel">导出excel</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>
      <el-card style="margin-top: 10px;">
        <el-table border :data="employeesList">
          <el-table-column type="index" label="序号" />
          <el-table-column prop="username" label="姓名" />
          <el-table-column label="头像" align="center">
            <template slot-scope="scope">
              <!-- <img class="staffPhoto" :src="scope.row.staffPhoto" alt=""> -->
              <image-holder :src="scope.row.staffPhoto" />
            </template>
          </el-table-column>
          <el-table-column prop="mobile" label="手机号" />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式" :formatter="formatter" />
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column label="入职时间" sortable>
            <template slot-scope="scope">
              <span>{{ transTime(scope.row.timeOfEntry) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="转正时间" sortable>
            <template slot-scope="scope">
              <span>{{ transTime(scope.row.correctionTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="$router.push('/employees/detail?id=' + scope.row.id)">查看</el-button>
              <el-button type="text" size="small" @click="setEmp(scope.row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="delEmployee(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页区域 -->
          <el-pagination
            :current-page="query.page"
            :page-sizes="[10, 15, 20, 25]"
            :page-size="query.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            @close="closeEmpDialog"
          />
        </el-row>
        <!-- 新增员工弹框组件 -->
        <el-dialog
          title="新增员工"
          :visible.sync="showDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          @close="closeEmpDialog"
        >
          <emp-dialog v-if="showDialog" @close="closeEmpDialog" />
        </el-dialog>

        <!-- 用户 分配角色 弹框组件 -->
        <el-dialog
          title="分配角色"
          :visible.sync="rolesDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          @close="closeRoles"
        >
          <assign-role v-if="rolesDialog" :roles-id="rolesId" @close="closeRoles" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employess'
import EmployeeEnum from '@/constant/employees'
import EmpDialog from './empDialog.vue'
import { transTime } from '@/utils'
import AssignRole from './assignRole.vue'
export default {
  name: 'Employees',
  components: {
    EmpDialog,
    AssignRole
  },
  data() {
    return {
      query: {
        page: 1, // 页码
        size: 10 // 每页条数
      },
      employeesList: [], // 员工列表
      total: 0, // 数据总条数
      showDialog: false, // 是否显示弹窗
      rolesDialog: false, // 角色管理
      rolesId: '' // 角色ID
    }
  },
  created() {
    // 调用获取员工信息列表
    this.getEmployeeList()
  },
  methods: {
    // 关闭角色管理窗口
    closeRoles() {
      this.rolesDialog = false
    },
    // setEmp 分配角色 权限
    setEmp(id) {
      this.rolesId = id
      this.rolesDialog = true
    },

    // 处理数据的函数
    transExcel(rows) {
      // 格式化的数据
      const map = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      }
      // 表头 数据可以冲第一行中 获取
      const headerKeys = Object.keys(rows[0])
      // 格式化成中文表头
      const header = headerKeys.map(item => map[item])
      // 获取所有的data 值
      const data = rows.map(item => Object.values(item))
      return { header, data }
    },

    // 导出excel
    async downloadExcel() {
      // 重新获取所有的数据
      const res = await getEmployeeList(this.query)
      // 定义一个方法 来格式化数据
      const { header, data } = this.transExcel(res.data.rows)
      import('@/vendor/Export2Excel').then(excel => {
        // excel表示导入的模块对象
        excel.export_json_to_excel({
          header: header, // 表头 必填
          data: data, // 具体数据 必填
          filename: '员工列表', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },

    // 时间格式化函数
    transTime,
    // 关闭弹窗 刷新数据
    closeEmpDialog() {
      // this.$refs.refFormD.resetFields()
      this.showDialog = false
      this.getEmployeeList()
    },

    // 删除员工信息
    async delEmployee(id) {
      const confirmRes = await this.$confirm('此操作将永久删除该员工, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return this.$message.info('取消删除员工')
      const res = await delEmployee(id)
      // 分页问题处理
      // 判断是不是最后一条数据
      if (this.employeesList.length === 1) {
        this.query.page--
        if (this.query.page <= 0) {
          this.query.page = 1
        }
      }
      this.$message.success(res.message)
      this.getEmployeeList()
    },

    // 聘用格式化
    formatter(row, column, cellValue, index) {
      const res = EmployeeEnum.hireType.find(item => item.id === cellValue)
      return res ? res.value : '未知'
    },

    // 获取员工信息列表
    async getEmployeeList() {
      const res = await getEmployeeList(this.query)
      // 格式化传递的数据
      res.data.rows.forEach(item => {
        // item.timeOfEntry = transTime(item.timeOfEntry)
        // item.correctionTime = transTime(item.correctionTime)

        item.formOfEmployment = +item.formOfEmployment
      })
      this.employeesList = res.data.rows
      this.total = res.data.total
    },

    // 每页显示的条数发生改变时触发
    handleSizeChange(newSize) {
      this.query.size = newSize
      this.getEmployeeList()
    },

    // 当前页面发生改变时触发
    handleCurrentChange(newPage) {
      this.query.page = newPage
      this.getEmployeeList()
    }
  }

}
</script>

<style lang="scss" scoped>
.staffPhoto {
  width: 100px;
  height: 100px;
  border-radius: 50%;
}
</style>
